﻿using Aspose.Cells;
using HDSmartCommunity.Models;
using Microsoft.AspNetCore.Http;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDSmartCommunity.BUS
{
    public class ParamsBUS
    {
        private static readonly object locker = new object();
        public ResponseMessage ExportParam(List<FloorAddress> list, string path, string type)
        {
            lock (locker)
            {
                ResponseMessage message = new ResponseMessage();
                try
                {
                    try
                    {
                        string sFileName = type + ".xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet

                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(type);

                            //添加头
                            worksheet.Cells[1, 1].Value = "ID";
                            worksheet.Cells[1, 2].Value = "地址";
                            //添加值

                            for (int i = 0; i < list.Count; i++)
                            {
                                FloorAddress address = list[i];
                                worksheet.Cells[i + 2, 1].Value = address.ID;
                                worksheet.Cells[i + 2, 2].Value = address.address;
                            }
                            package.Save();

                        }
                        message.errcode = 0;
                        message.errmsg = "";
                        message.data = "/" + sFileName;
                    }
                    catch (Exception ex)
                    {
                        message.errcode = 1;
                        message.errmsg = "导出失败";
                    }

                    return message;

                }
                catch
                {
                    message.errcode = 1;
                    message.errmsg = "导出失败";
                }

                return message;
            }
        }

        public string UploadFileToServer(IFormFile file, string webRootPath)
        {
            string result = string.Empty;
            try
            {
                String Tpath = "/File/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string name = file.FileName;
                string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string FilePath = webRootPath + Tpath;

                string type = Path.GetExtension(name);
                DirectoryInfo di = new DirectoryInfo(FilePath);

                if (!di.Exists) { di.Create(); }

                using (FileStream fs = File.Create(FilePath + FileName + type))
                {
                    // 复制文件
                    file.CopyTo(fs);
                    fs.Flush();
                    fs.Close();
                }
                result = FilePath + FileName + type;

            }
            catch (Exception e)
            {
                result = "fail,文件导入失败" + e.ToString();
            }

            return result;
        }

        public DataTable ImportCompareMeanExcel(string strFilePath)
        {
            Workbook workbook = new Workbook(strFilePath);
            DataTable dtExcel = null;
            try
            {
                Cells cells = workbook.Worksheets[0].Cells;
                dtExcel = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, true);//showTitle
            }
            catch (Exception ex)
            {
                return null;
            }
            return dtExcel;
        }

        public List<FloorAddress> GetFloorAddress(string strFilePath)
        {
            List<FloorAddress> list = new List<FloorAddress>();
            DataTable data;
            data = ImportCompareMeanExcel(strFilePath);
            if (data != null)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    FloorAddress address = new FloorAddress();
                    address.ID = Convert.ToInt32(data.Rows[i][0]);
                    address.address = data.Rows[i][1].ToString();
                    list.Add(address);
                }
            }
            return list;
        }
    }
}
